﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Game;

namespace Memory.WindowsUI
{
    public class FormGameSettings : Form
    {
        private const string k_AgainstAFriendStr = "Against a Friend";
        private const string k_AgainstComputerStr = "Against Computer";
        private const string k_FormTitleStr = "Memory Game - Settings";
        private const string k_DefaultSinglePlayerText = "-Computer-";

        private GameSettings m_GameSettings;

        private Label labelFirstPlayer = new Label();
        private Label labelSecondPlayer = new Label();
        private Label labelBoardSize = new Label();

        private TextBox textBoxFirstName = new TextBox();
        private TextBox textBoxSecondName = new TextBox();

        private Button buttonAgainstWho = new Button();
        private Button buttonBoardSize = new Button();
        private Button buttonOK = new Button();

        private int m_CurrentBoardSizeIndex;

        public FormGameSettings()
        {
            this.Size = new Size(350, 200);
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = k_FormTitleStr;

            m_CurrentBoardSizeIndex = 0;

            m_GameSettings = new Game.GameSettings();
            initSettings();

            buttonBoardSize.Click += new EventHandler(buttonBoardSize_Click);
            buttonAgainstWho.Click += new EventHandler(buttonAgainstWho_Click);
            buttonOK.Click += new EventHandler(buttonOK_Click);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool isValid = true;
            if (textBoxFirstName.Text == string.Empty)
            {
                isValid = false;
                MessageBox.Show("Please enter a name for the first player", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                m_GameSettings.Player1Name = textBoxFirstName.Text;
            }

            if (!m_GameSettings.IsSinglePlayer)
            {
                if (textBoxSecondName.Text == string.Empty)
                {
                    isValid = false;
                    MessageBox.Show("Please enter a name for the second player");
                }
                else
                {
                    m_GameSettings.Player2Name = textBoxSecondName.Text;
                } 
            }

            if (isValid)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void initSettings()
        {
            m_GameSettings.Rows = 4; 
            m_GameSettings.Cols = 4;

            m_GameSettings.IsSinglePlayer = true;
        }

        private void buttonAgainstWho_Click(object sender, EventArgs e)
        {
            if (buttonAgainstWho.Text == k_AgainstAFriendStr)
            {
                buttonAgainstWho.Text = k_AgainstComputerStr;
                textBoxSecondName.Enabled = true;
                textBoxSecondName.Text = string.Empty;
                m_GameSettings.IsSinglePlayer = false;
            }
            else
            {
                buttonAgainstWho.Text = k_AgainstAFriendStr;
                textBoxSecondName.Enabled = false;
                textBoxSecondName.Text = k_DefaultSinglePlayerText;
                m_GameSettings.IsSinglePlayer = true;
            }
        }

        private void buttonBoardSize_Click(object sender, EventArgs e)
        {
            m_CurrentBoardSizeIndex = (m_CurrentBoardSizeIndex + 1) % Board.ValidBoardSizes.Length;
            m_GameSettings.Rows = Board.ValidBoardSizes[m_CurrentBoardSizeIndex].Rows;
            m_GameSettings.Cols = Board.ValidBoardSizes[m_CurrentBoardSizeIndex].Cols;

           buttonBoardSize.Text = string.Format("{0} x {1}", m_GameSettings.Rows, m_GameSettings.Cols);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            initControls();
        }

        private void initControls()
        {
            labelFirstPlayer.Text = "First Player Name:";
            labelFirstPlayer.AutoSize = true;
            labelFirstPlayer.Location = new Point(10, 20);

            labelSecondPlayer.Text = "Second Player Name:";
            labelSecondPlayer.AutoSize = true;
            labelSecondPlayer.Location = new Point(labelFirstPlayer.Location.X, labelFirstPlayer.Bottom + 8);

            labelBoardSize.Text = "Board Size:";
            labelBoardSize.Location = new Point(labelSecondPlayer.Location.X, labelSecondPlayer.Bottom + 8);

            int textBoxFirstNameTop = labelFirstPlayer.Top + (labelFirstPlayer.Height / 2);
            textBoxFirstNameTop -= textBoxFirstName.Height / 2;

            textBoxFirstName.Location = new Point(labelSecondPlayer.Right + 16, textBoxFirstNameTop);

            int textBoxSecondNameTop = labelSecondPlayer.Top + (labelSecondPlayer.Height / 2);
            textBoxSecondNameTop -= textBoxSecondName.Height / 2;

            textBoxSecondName.Location = new Point(labelSecondPlayer.Right + 16, textBoxSecondNameTop);
            textBoxSecondName.Enabled = false;
            textBoxSecondName.Text = k_DefaultSinglePlayerText;

            int buttonAgainstWhoTop = labelSecondPlayer.Top + (labelSecondPlayer.Height / 2);
            buttonAgainstWhoTop -= buttonAgainstWho.Height / 2;

            buttonAgainstWho.Text = k_AgainstAFriendStr;
            buttonAgainstWho.AutoSize = true;
            buttonAgainstWho.Location = new Point(textBoxSecondName.Right + 8, buttonAgainstWhoTop);

            buttonBoardSize.Text = string.Format("{0} x {1}", m_GameSettings.Cols, m_GameSettings.Rows);
            buttonBoardSize.Size = new Size(100, 60);
            buttonBoardSize.BackColor = Color.Lavender;
            buttonBoardSize.Location = new Point(
                labelBoardSize.Location.X,
                labelBoardSize.Location.Y + labelBoardSize.Height);

            buttonOK.Text = "Start!";
            buttonOK.BackColor = Color.LawnGreen;
            buttonOK.Location = new Point(
                this.ClientSize.Width - buttonOK.Width - 8,
                this.ClientSize.Height - buttonOK.Height - 8);

            this.AcceptButton = buttonOK;

            this.Controls.AddRange(new Control[] 
            { 
                labelFirstPlayer, 
                labelSecondPlayer, 
                labelBoardSize, 
                textBoxFirstName, 
                textBoxSecondName, 
                buttonAgainstWho, 
                buttonBoardSize, 
                buttonOK 
            });
        }
        
        public GameSettings GameSettings
        {
            get { return m_GameSettings; }
        }
    }
}
